/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : DigitValueProcessHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Dec 2, 2008       Raghu Sarraff			       Created
 */
package net.lwf.handler.math.number;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import lwf.handler.IProcessHandler;
import lwf.handler.ProcessHandler;
import lwf.handler.entities.RadioData;
import lwf.handler.helper.DisplayFormatter;
import net.lwf.entity.QuestionInfo;
import net.lwf.number.util.NumberFormatter;
import net.lwf.number.util.RandomNumberGenerator;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;

/**
 * Type the class description here
 *  
 */
public class DigitValueProcessHandler extends ProcessHandler implements IProcessHandler {

	public DisplayFormatter getDisplayFormatter() {
		
		return null;
	}

	public String getQuestionDesc() {
		
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		return this.generateSingle(false);
	}
	
	private QuestionInfo generateSingle(boolean isMultiple) {
		
		//"Find the value of number, " + getData1() + " in the following number."
		
		QuestionInfo qinfo = new QuestionInfo();
		String sNumber = String.valueOf(RandomNumberGenerator.generateInt(1, 99999));
		qinfo.setQuestion(NumberFormatter.format(sNumber));
		int pos = RandomNumberGenerator.generate(1, sNumber.length());
		int remaining = (sNumber.substring(pos, sNumber.length())).length();
		String numbReqd = String.valueOf(sNumber.charAt(pos-1));
		qinfo.setPlaceHolder("Find the value of digit, " + numbReqd + " in the following number.");
		setData1(String.valueOf(numbReqd));
		int nNumbRequired = Integer.parseInt(numbReqd);
		String sFactor = "1";
		for(int i=0; i<remaining; i++) {
			sFactor += "0";
		}
		int answer = Integer.parseInt(sFactor) * nNumbRequired;
		qinfo.setAnswer(String.valueOf(answer));
		qinfo.setRadioAnswerChoices(this.getRadioAnswers(String.valueOf(answer)));		
		return qinfo;
	}

	private List<RadioData> getRadioAnswers(String answer) {
		List<RadioData> rdata = new ArrayList<RadioData>();
		rdata.add(new RadioData(answer, NumberFormatter.format(answer)));
		int nAnswer = Integer.parseInt(answer);
		rdata.add(new RadioData(String.valueOf(10*nAnswer), NumberFormatter.format(String.valueOf(10*nAnswer))));
		rdata.add(new RadioData(String.valueOf(100*nAnswer), NumberFormatter.format(String.valueOf(100*nAnswer))));		
		rdata.add(new RadioData(String.valueOf(1000*nAnswer), NumberFormatter.format(String.valueOf(1000*nAnswer))));	
		Collections.shuffle(rdata);
		
		return rdata;
	}
	
	@Override
	public List<QuestionInfo> generateMultiple() {
		List<QuestionInfo> questions = new ArrayList<QuestionInfo>();
		int nQuestions = getRows() * getColumns();
		for(int i=0; i<nQuestions; i++) {
			QuestionInfo question = generateSingle(true);
			question.setQuestionNumber(i+1);
			questions.add(question);
		}
		return questions;
	}

	@Override
	public void getContent(Document document) throws DocumentException {
		
		
	}

	@Override
	public int getRows() {
		
		return 0;
	}

	@Override
	public int getColumns() {
		
		return 0;
	}

	@Override
	public boolean isPortrait() {
		
		return false;
	}

	@Override
	public String getPdfInstructionalText() {
		
		return null;
	}

	@Override
	public String getPdfNote() {
		
		return null;
	}
	
	
}

